// 常量定义
const CONFIG_KEYS = {
    PRODUCT: "Product",
    MOBILE: "Mobile",
    TOKEN: "Token",
    USER_ID: "UserId",
    ADVERT_ID: "AdvertId",
    ANDROID_ID: "AndroidId",
    Country: "Country"
};

// 默认配置列表
const DEFAULT_CONFIG_LIST = [
    { label: CONFIG_KEYS.PRODUCT, hint: CONFIG_KEYS.PRODUCT },
    { label: CONFIG_KEYS.MOBILE, hint: CONFIG_KEYS.MOBILE },
    { label: CONFIG_KEYS.TOKEN, hint: CONFIG_KEYS.TOKEN },
    { label: CONFIG_KEYS.USER_ID, hint: CONFIG_KEYS.USER_ID },
    { label: CONFIG_KEYS.ADVERT_ID, hint: CONFIG_KEYS.ADVERT_ID },
    { label: CONFIG_KEYS.ANDROID_ID, hint: CONFIG_KEYS.ANDROID_ID }
];

// 常量定义
const COUNTRY_KEYS = {
    KSH: { value: "ksh", label: "肯尼亚" },
    GHA: { value: "gha", label: "加纳" },
    UGX: { value: "ugx", label: "乌干达" },
    ZMB: { value: "zmb", label: "赞比亚" },
    FCFA: { value: "fcfa", label: "科特" },
    XAF: { value: "xaf", label: "喀唛隆" },
    SENEGAL: { value: "senegal", label: "塞内" },
    BENIN: { value: "benin", label: "贝宁" },
    CONGO: { value: "congo", label: "刚果金" },
    TANZ: { value: "tanz", label: "坦桑" },
    MOZAMBIQUE: { value: "mozambique", label: "莫桑" },
    CAMBODAI: { value: "cambodia", label: "柬埔寨" },
    SRILANKA: { value: "srilanka", label: "斯里兰卡" },
};

/**
 * 获取默认国家列表
 * @returns {Array<{value: string, label: string, checked: boolean}>} 国家列表
 */
const DEFAULT_COUNTRY_LIST = () => {
    return Object.values(COUNTRY_KEYS).map(item => ({
        ...item,
        checked: false
    }));
};

// 通用的缓存设置函数
export const setCache = (key, value) => {
    try {
        localStorage.setItem(key, value || "");
    } catch (error) {
        console.error(`Failed to set cache for ${key}:`, error);
    }
};

// 通用的缓存获取函数
export const getCache = (key) => {
    try {
        return localStorage.getItem(key) || "";
    } catch (error) {
        console.error(`Failed to get cache for ${key}:`, error);
        return "";
    }
};

// 导出具体的缓存设置函数
export const setProductCache = (value) => setCache(CONFIG_KEYS.PRODUCT, value);
export const setMobileCache = (value) => setCache(CONFIG_KEYS.MOBILE, value);
export const setTokenCache = (value) => setCache(CONFIG_KEYS.TOKEN, value);
export const setUserIdCache = (value) => setCache(CONFIG_KEYS.USER_ID, value);
export const setAdvertIdCache = (value) => setCache(CONFIG_KEYS.ADVERT_ID, value);
export const setAndroidIdCache = (value) => setCache(CONFIG_KEYS.ANDROID_ID, value);
export const setCountryCache = (value) => setCache(CONFIG_KEYS.Country, value);

// 导出具体的缓存获取函数
export const getProductCache = () => getCache(CONFIG_KEYS.PRODUCT);
export const getMobileCache = () => getCache(CONFIG_KEYS.MOBILE);
export const getTokenCache = () => getCache(CONFIG_KEYS.TOKEN);
export const getUserIdCache = () => getCache(CONFIG_KEYS.USER_ID);
export const getAdvertIdCache = () => getCache(CONFIG_KEYS.ADVERT_ID);
export const getAndroidIdCache = () => getCache(CONFIG_KEYS.ANDROID_ID);
export const getCountryCache = () => getCache(CONFIG_KEYS.Country);

// 获取所有配置的缓存值
export const getConfigsCache = () => {
    return DEFAULT_CONFIG_LIST.map(item => ({
        ...item,
        value: getCache(item.label),
        checked: false
    }));
};

// 获取所有配置的缓存值
export const getCountrysCache = () => {
    let country = getCountryCache();
    return DEFAULT_COUNTRY_LIST().map(item => ({
        ...item,
        checked: item.value === country
    }));
};

// 清除所有配置的缓存
export const clearConfigCache = () => {
    Object.values(CONFIG_KEYS).forEach(key => {
        try {
            localStorage.removeItem(key);
        } catch (error) {
            console.error(`Failed to clear cache for ${key}:`, error);
        }
    });
};


// 获取请求参数
export const getApiParms = (type = "") => {
    let country = getCountryCache();
    let product = getProductCache();
    let mobile = getMobileCache();
    let token = getTokenCache();
    let userId = getUserIdCache();
    let advertId = getAdvertIdCache();
    let androidId = getAndroidIdCache();

    return {
        type: type,
        country: country,
        product: product,
        username: mobile,
        token: token,
        userId: userId,
        advertId: advertId,
        androidId: androidId
    }
}